package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * CTassName: DishController
 * Package: com.sky.controller.admin
 * Description:
 * xw
 * 2023/11/15 15:10
 * 1.0
 */

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品接口")
public class DishController {

    // 菜品，业务层
    @Autowired
    private DishService dishService;

    // 菜品分页查询
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {

        log.info("菜品分页查询：{}", dishPageQueryDTO);

        // 调用业务层
        PageResult pageResult = dishService.page(dishPageQueryDTO);

        return Result.success(pageResult);
    }


    // 新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    Result<String> save(@RequestBody DishDTO dishDTO) {

        log.info("新增菜品：{}", dishDTO);
        // 调用业务层，新增菜品
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();


        return Result.success();
    }

    // 批量删除菜品
    @DeleteMapping()
    @ApiOperation("批量删除菜品")
    Result<String> delete(String ids) {

        log.info("批量删除菜品：{}", ids);

        // 调用业务层，批量删除菜品
        dishService.deleteDish(ids);


        return Result.success();
    }


    // 根据 id 查询菜品
    @GetMapping("/{id}")
    @ApiOperation("根据 id 查询菜品")
    Result<DishVO> getById(@PathVariable Long id) {

        log.info("根据 id 查询菜品：{}", id);
        // 调用业务层，根据 id 查询菜品
        DishVO dishVO = dishService.getById(id);

        return Result.success(dishVO);
    }


    // 修改菜品
    @PutMapping
    @ApiOperation("修改菜品")
    Result<String> update(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品：{}", dishDTO);
        // 调用业务层，修改菜品
        dishService.updateWithFlavor(dishDTO);

        return Result.success();
    }


    // 菜品 （启售，禁售）
    @PostMapping("/status/{status}")
    @ApiOperation("菜品禁用")
    Result<String> DishStartAndStop(@PathVariable Integer status, Long id) {

        log.info("菜品禁用：status = {},id = {}", status, id);
        // 调用业务层，菜品禁用
        dishService.DishStartAndStop(status, id);

        return Result.success();
    }


    // 根据分类id查询菜品
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    Result<List<Dish>> IdDishesByCategory(@RequestParam("categoryId") Long id) {

        log.info("根据分类id查询菜品 {}", id);
        // 调用业务层，根据分类id查询菜品
        List<Dish> dish = dishService.IdDishesByCategory(id);
        return Result.success(dish);
    }
}
